import time
import requests
myHeaders={'User-Agent':'Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/63.0.3239.132 Safari/537.36 QIHU 360SE'}

def getHtmlText(url):
    try:
        r=requests.get(url,timeout=30,headers=myHeaders)
        r.raise_for_status()
        r.encoding=r.apparent_encoding
        return r.text
    except :
        return "产生异常"

if __name__ == '__main__':
    url = "https://book.douban.com/subject/1255625/comments/"
    url1 = "https://book.douban.com/subject/1255625/comments/?start="
    url2 = "&limit=20&status=P&sort=new_score"
    txtComment = getHtmlText(url)
    for i in range(0,5):
        path='评论第{}页.txt'.format(i)
        with open(path,'w',encoding='utf-8') as file:
            file.write(txtComment)
        time.sleep(3)
        txtComment = getHtmlText(url1 + str(i * 20) + url2)